package com.zkingsoft.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.CompanyDao;
import com.zkingsoft.model.Company;
import com.zkingsoft.model.Role;
import com.zkingsoft.model.RolePwoer;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.services.CompanyService;
import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.RoleService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-04-22 08:43
 */
@Service("companyService")
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyDao companyDao;

	@Autowired
	private RolePwoerService rolePwoerService;

	@Autowired
	private RoleService rolerService;

	@Override
	public int add(Company company) {

		return companyDao.insert(company);

	}

	/**
	 * 同时更新改企业下所有角色信息
	 */
	@Transactional
	@Override
	public int modify(Company company) {
		List<Long> oldFunctions=null;
		String functions=companyDao.selectById(company.getId()).getFunctions();
		if(functions==null || functions.equals("")){
			oldFunctions=new ArrayList<>();
		}else{ 
			oldFunctions = StringUtils.strToCollToLong(companyDao.selectById(company.getId()).getFunctions(),",");
		}
				
		List<Long> newFunctions = StringUtils.strToCollToLong(company.getFunctions(), ",");
		for (Long old : oldFunctions) {
			// 如果新权限中不包含这个老的功能，则要更新改企业下所有的角色权限
			if (!newFunctions.contains(old)) {
				Role role = new Role();
				role.setCompanyId(company.getId());
				List<Role> roles = rolerService.findByModel(role);
				// 获取该公司下所有的权限信息
				List<RolePwoer> compalyRolePwoer = new ArrayList<>();
				for (Role tempRole : roles) {
					RolePwoer r = new RolePwoer();
					r.setRoleId(tempRole.getId());
					compalyRolePwoer.addAll(rolePwoerService.findByModel(r));
				}
				// 记录要删除的id
				List<Long> delRolePwoer = new ArrayList<>();
				for (RolePwoer rolePwoer : compalyRolePwoer) {
					// 如果有一个老权限则删除这个权限
					if (rolePwoer.getFunctionId()!=null && rolePwoer.getFunctionId().equals(old)) {
						delRolePwoer.add(rolePwoer.getId());
					}
				}
				// 如果获取到了就删除
				if (delRolePwoer.size() > 0) {
					rolePwoerService.remove(delRolePwoer);
				}
			}
		}
		return companyDao.update(company);
	}

	@Override
	public int remove(List<Long> list) {

		return companyDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long id) {

		return companyDao.deleteById(id);

	}

	@Override
	public List<Company> findInPage(Company company, PaginationVO pageVo) {

		return companyDao.selectInPage(company, pageVo);

	}

	@Override
	public List<Company> findByModel(Company company) {

		return companyDao.selectByModel(company);

	}

	@Override
	public int findTotal(Company company) {

		return companyDao.selectTotalRecord(company);

	}

	@Override
	public Company findById(Long id) {

		return companyDao.selectById(id);

	}

	/**
	 * 检测重复，不包括对自己的检测
	 */
	@Override
	public boolean checkIsRepeat(Company company) {
		if (company.getName() == null || company.getName().equals("")) {
			return true;
		} else {
			Company c = new Company();
			c.setName(company.getName());
			List<Company> list = companyDao.selectByModel(c);
			if (list == null || list.size() == 0) {
				return false;
			} else {
				Company cin = list.get(0);
				if (cin.getId().equals(company.getId()) ) {
					return false;
				} else {
					return true;
				}
			}
		}
	}

}